import axios from 'axios'
import Vue from 'vue'
import Vuex from 'vuex'
import { getComingSoonAPI } from "@/network/api";
import {
  COMINGLIST_MUTATION,
} from "@/store/mutations-types.js"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    comingList: [],
    comingListPage: 1,
    comingListTotal: 0,
    isBottom: false,
  },
  mutations: {
    // 将actions里面获取的数据保存到state里面
    // mutation事件名(state,payload){state.comingList = payload}
    [COMINGLIST_MUTATION](state, payload) {
      // state.comingList = payload
      state.comingList = [...state.comingList, ...payload]
      state.comingListPage++
    },
  },
  actions: {
    // 【2】active事件名(context)接收事件
    getComingListAction(context) {
      // axios异步请求，请求的数据再发送给mutations
      getComingSoonAPI({
        cityId: 110100,
        pageNum: context.state.comingListPage,
        pageSize: 10,
        type: 2,
        k: 7541851
      }).then(res => {
        // 【4】 context.commit('mutation事件名',axios数据)
        context.commit(COMINGLIST_MUTATION, res.data.data.films)

        if (res.data.data.films.length == 0) {
          context.state.isBottom = true
        }
      })
    }
  },
  modules: {
  }
})
